package com.bangdream.michelia.view.fragment.main.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.entity.LearningBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.presenter.base.BasePresenter;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.view.activity.curriculum.CurriculumDetails;
import com.bangdream.michelia.view.adapter.LearningRecordAdapter;
import com.bangdream.michelia.view.adapter.MyLearningRecordsAdapter;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

/**
 * @date on 2018/7/23 23:54
 * @email 909732184@qq.com
 * 学习记录列表
 */
public class LearningRecordsList extends LazyLoadFragment<CoursesContract.ICourseLearning, CoursesPresenter<CoursesContract.ICourseLearning>>
        implements CoursesContract.ICourseLearning {
    private View view;
    private XRefreshView refreshView;
    private RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private LearningRecordAdapter adapter;
    private int page = 1;
    int type = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recycler_view_layout, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }


    @Override
    public CoursesPresenter<CoursesContract.ICourseLearning> createPresenter() {
        return new CoursesPresenter<>();
    }

    @Override
    public void loadData() {
        adapter.setEmptyView(loadingView);
        mPresenter.getLearning(page, PublicResources.PageSize, type);
    }

    private void init() {
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }

        refreshView = view.findViewById(R.id.refreshView);
        refreshView.setPullRefreshEnable(false);
        refreshView.setPullLoadEnable(false);
        recyclerView = view.findViewById(R.id.recyclerView);
        initEmptyView(recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new LearningRecordAdapter(R.layout.learning_records_adapter_item);
        recyclerView.setAdapter(adapter);

        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                super.onRefresh(isPullDown);
                page = 1;
                loadData();
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                page++;
                loadData();
            }
        });
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter a, View view, int position) {
                Intent intent = new Intent(getActivity(), CurriculumDetails.class);
                intent.putExtra("id", adapter.getData().get(position).getId());
                intent.putExtra("title", adapter.getData().get(position).getName());
                intent.putExtra("teacherId", "");
                startActivity(intent);


            }
        });
    }

    @Override
    public void setLearningList(boolean isSuccess, List<LearningBean> data) {
        if (isSuccess && data != null) {
            if (page == 1) {
                adapter.setNewData(data);
                adapter.notifyDataSetChanged();
                refreshView.stopRefresh();
                //显示数据大于20条才允许上拉加载更多
                if (data.size() >= PublicResources.PageSize) {
                    refreshView.setPullLoadEnable(true);
                }
                if (data.size() == 0) {
                    adapter.setEmptyView(noDataView);
                }
            } else {
                adapter.addData(data);
                refreshView.stopLoadMore();
            }
        } else {
            //如果是加载新数据时失败了 则page-1
            if (page > 1) {
                refreshView.stopLoadMore();
                this.page--;
            } else {
                //还是还原为1防止出现负数
                this.page = 1;
                refreshView.stopRefresh();
                adapter.setEmptyView(isSuccess ? noDataView : errorView);
            }
        }
    }

    //回调用来接收参数
    public LearningRecordsList getType(int type) {
        LearningRecordsList fragment = new LearningRecordsList();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }
}
